//
//  SingleDetailVC.swift
//  YueChat
//
//  Created by IMac on 2024/10/8.
//

import UIKit

class SingleDetailVC: BaseVC {

    var userInfo = DMCCUser()
    var con = DMCCConversation()
    
    init(userInfo: DMCCUser = DMCCUser(),con: DMCCConversation = DMCCConversation()) {
        super.init(nibName: nil, bundle: nil)
        self.userInfo = userInfo
        self.con = con
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.updateUserinfo()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "聊天详情"
        setup()
    }
    
    func updateUserinfo () {
        
        let user = gosnUser.getUserInfoDefault(self.userInfo.userId)
        self.userInfo = user
        GK_main_async_safe {
            self.tableView.reloadData()
        }
        
//        gosnUser.getUserInfo(self.userInfo.userId, refresh: false) { a in
//            if a != nil {
//                self.userInfo = a as! UserInfo
//                GK_main_async_safe {
//                    self.tableView.reloadData()
//                }
//            }
//        } error: { e in
//
//        }

    }


    func setup () {
//        dataSource = [["head","免打扰","置顶"],["查找聊天记录"],["推荐给好友","清空聊天记录"],["投诉"],["删除会话"]]
        dataSource = [["head","免打扰","置顶"],["查找聊天记录"],["推荐给好友"],["投诉"],["清空聊天记录"]]
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UINib(nibName: "UserCell", bundle: nil), forCellReuseIdentifier: "UserCell")
        tableView.register(UINib(nibName: "SubTitleCell", bundle: nil), forCellReuseIdentifier: "SubTitleCell")
        tableView.register(UINib(nibName: "SwitchCell", bundle: nil), forCellReuseIdentifier: "SwitchCell")
        tableView.register(UINib(nibName: "LogoutCell", bundle: nil), forCellReuseIdentifier: "LogoutCell")
    }
    

  
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.section == 0 && indexPath.row == 0 {
            return 120
        }
        
        return 60
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let array = dataSource[section] as! Array<String>
        return array.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.001
    }
    
//    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        let view = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 10))
//        view.backgroundColor = UIColor.lightGray
//        return view
//    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
        let array = self.dataSource[indexPath.section] as! Array<String>
        let name = array[indexPath.row]
        if indexPath.section == 0 && indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserCell
            cell.setUserInfo(userInfo: self.userInfo)
            cell.imgWidth.constant = 70
            cell.imgHeight.constant = 70
            return cell
        }
        
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchCell", for: indexPath) as! SwitchCell
            cell.titleLab.text = name
            
            if indexPath.row == 1 {
                cell.type = .NoticeType
                cell.con = self.con
                cell.sw.setOn(self.con.isSilent, animated: true)
            }else{
                cell.type = .TopType
                cell.con = self.con
                cell.sw.setOn(self.con.isTop, animated: true)
            }
            
            return cell
        }
        
//        dataSource = [["head","免打扰","置顶"],["查找聊天记录"],["推荐给好友","清空聊天记录"],["投诉"],["删除会话"]]
        
        if  indexPath.section == 4{
            let cell = tableView.dequeueReusableCell(withIdentifier: "LogoutCell", for: indexPath) as! LogoutCell
            cell.titleLab.text = name
            
            cell.titleLab.textColor = UIColor.red
            return cell
        }
        
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.contentView.backgroundColor = UIColor.clear
        cell.accessoryType = .disclosureIndicator
        cell.textLabel?.text = name
        return cell
        
        
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
        
        
        if (indexPath.section == 0  && indexPath.row == 1) {
            self.presentVC(VC: SetNickNameVC(type: .user, SId: self.userInfo.userId, dismissCompletion: {
                self.updateUserinfo()
            }))
        }  else if indexPath.section == 1 {
            navigationController?.pushViewController(ChatSearchVC.init(self.con), animated: true)
        } else if (indexPath.section == 2) {//推荐
            let vc = ShareUserVC(curentUser: self.userInfo, curentGroup: DMCCGroup())
            vc.isContact = true
            self.presentVC(VC: vc)
            
        } else if (indexPath.section == 3) {
            self.navigationController?.pushViewController(ReportVC(isGroup: false), animated:  true)
        } else if indexPath.section == 4 {
            let str = KLocalizedString("确定清空聊天记录吗")
            MHAlert.showCustomAlert(str) { ac in
                if ac.title == KLocalizedString("Sure") {
                    self.clearChat()
                }
            }
        } 
    }
    
    
    func clearChat() {
        _ = gosnMessage.clearMessage(con.targetId)
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            MHAlert.showMessage("清空完成")
        }
    }

    
    deinit {
        
    }
}
